<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="css/header.css" />

	</head>

	<body>
		<header class="mui-bar mui-bar-nav title">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: white"></a>
			<a id="link_submit_alias" class="mui-icon mui-icon-right-nav mui-pull-right" style="color: white;font-size: 14px;line-height: 25px;">完成</a>
			<h1 class="mui-title title-color">设置备注</h1>
		</header>

		<div class="mui-content">
			<div class="mui-input-row">
				<input id="txt_alias" type="text" class="mui-input-speech mui-input-clear" placeholder="修改昵称">
			</div>

		</div>

		<script src="js/mui.min.js"></script>
		<script type="text/javascript" src="js/app.js"></script>
		<script type="text/javascript">
			mui.init()

			mui.plusReady(function() {
				var user = app.getUserGlobalInfo();
				var aliasWebview = plus.webview.currentWebview();
				var friendAlias = aliasWebview.friendAlias;
				var friendId = aliasWebview.friendId;
				var txt_alias = document.getElementById("txt_alias");
				txt_alias.value = friendAlias;

				//提交事件，修改备注
				var link_submit_alias = document.getElementById("link_submit_alias");
				link_submit_alias.addEventListener("tap", function() {
					var newAlias = txt_alias.value;
					if(newAlias.length > 12) {
						app.showToast("备注不能超过12位", "error");
						return false;
					}
					if(newAlias.length < 1) {
						app.showToast("备注长度太短", "error");
						return false;
					}

					plus.nativeUI.showWaiting("请稍后..");
					mui.ajax(app.serverUrl + "/user/setAlias", {
						data: {
							userId: user.id,
							friendId: friendId,
							alias: txt_alias.value
						},
						dataType: 'json', //服务器返回json格式数据
						type: 'post', //HTTP请求类型
						timeout: 10000, //超时时间设置为10秒；
						headers: {
							'Content-Type': 'application/json'
						},
						success: function(data) {
							plus.nativeUI.closeWaiting();
							if(data.success) {
								//刷新缓存
								app.showToast("修改成功！", "success");
								app.setContactList(data.data);
								// 触发另外一个webview的自定义事件，可以使用 mui.fire()
								var detail = plus.webview.getWebviewById("detail.html");
								mui.fire(detail, "refresh", {
									friendId: friendId
								});
								mui.back();
							} else {
								app.showToast("修改失败！", "error");
							}
						}

					});
				});
			});
		</script>

	</body>

</html>